<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
    <body>
        <ui:composition template="../templates/default3.xhtml">  
            <ui:define name="content">
                <h:form id="loadUpdateForecast" binding="#{salesForecastManagerBean.loadUpdateForecast}">

                    <p:messages id="error"/>


                    <p:panel header="Sales History #{salesForecastManagerBean.salesForecast.product.name}" >
                        <p:dataTable id="salesHistorydt" var="salesHistory" value="#{salesForecastManagerBean.salesHistories}" 
                                     emptyMessage="No sales history found with given criteria." paginator="true" rows="6"  
                                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} " >

                            <p:column filterBy="#{salesHistory.salesMonth}" headerText="Month" 
                                      filterMatchMode="contains" sortBy="#{salesHistory.salesMonth}" style="width:30%">  
                                <center><h:outputText value="#{salesHistory.salesMonth}"/></center>
                            </p:column> 
                            <p:column filterBy="#{salesHistory.salesYear}" headerText="Year" 
                                      filterMatchMode="contains" sortBy="#{salesHistory.salesYear}" style="width:30%">  
                                <center><h:outputText value="#{salesHistory.salesYear}"/></center>
                            </p:column> 

                            <p:column  headerText="Demand month" >  
                                <center> <h:outputText value="#{salesHistory.quantity}"/> </center>
                            </p:column> 
                            <p:columnGroup type="footer">  
                                <p:row>  
                                    <p:column colspan="2" footerText="Demand mean:" style="text-align:right"/>
                                    <p:column footerText="#{salesForecastManagerBean.salesMeanHistory}"/>

                                </p:row>   
                                <p:row> 
                                    <p:column colspan="2" footerText="Demand median:" style="text-align:right"/>
                                    <p:column footerText="#{salesForecastManagerBean.salesMedianHistory}"/>

                                </p:row>  
                                <p:row> 
                                    <p:column colspan="2" footerText="Demand Total:" style="text-align:right"/>
                                    <p:column footerText="#{salesForecastManagerBean.salesTotalHistory}"/>

                                </p:row>   

                            </p:columnGroup>  



                        </p:dataTable>

                    </p:panel>



                    <p:panel header="Demand vs 3 months moving average" >
                        <p:lineChart var ="chart" id="salesHistoryChart" model="#{salesForecastManagerBean.model}" width ="700px"
                                     titleX ="Month" titleY="Demand">
                            <p:chartSeries label="Demand" value="#{chart.demandCurrentYear}" /> 
                        </p:lineChart>
                    </p:panel>

                    <p:panel header="Current production plan - #{salesForecastManagerBean.salesForecast.salesMonth}/#{salesForecastManagerBean.salesForecast.salesYear}"  id="forecastDisplay">

                        <table>
                            <tr>
                                <td>Number of production line:</td>
                                <td>
                                    <h:outputText value="#{salesForecastManagerBean.line}"/>
                                </td>
                            </tr>
                            <tr>
                                <td>Average utilization rate:</td>
                                <td>
                                    <h:outputText value="#{salesForecastManagerBean.aveUtilization}"/>
                                </td>
                            </tr>
                            <tr>
                                <td><b>Production details:</b></td>
                                <td></td>
                            </tr>

                        </table>
                        
                        <hr/>
                          <p:dataTable id="productionScheduleDay" var="productionScheduleDay" value="#{salesForecastManagerBean.productionScheduleDays}" 
                                                 emptyMessage="No production schedule found with given criteria.">

                                        <p:column headerText ="Product">  
                                            <center> <h:outputText value="#{productionScheduleDay.product.name}"/> </center>
                                        </p:column> 
                                        <p:column headerText ="Quantity">  
                                            <center> <h:outputText value="#{productionScheduleDay.quantity}"/> </center>
                                        </p:column> 
                                    </p:dataTable>






                    </p:panel>




                    <p:panel header="Forecast input" id="forecastInput">

                        <table>
                            <tr>
                                <td>
                                    Forecasting date:
                                </td>
                                <td>
                                   <h:outputText value="#{salesForecastManagerBean.salesForecast.salesMonth}/#{salesForecastManagerBean.salesForecast.salesYear}"/>
                                </td>

                            </tr>
                            <tr><td>Product:</td>
                                <td><b><h:outputText value="#{salesForecastManagerBean.salesForecast.product.name}"/></b></td>
                            </tr>
                            <tr>
                                <td>
                                    Sales forecast (Plan):

                                </td>
                                <td>
                                    <p:inputText id ="quantity" value="#{salesForecastManagerBean.salesForecast.quantity}" />
                                </td>
                            </tr>
                            <tr>
                                <td valign ="top">
                                    Remark:
                                </td>
                                <td>
                                    <p:inputTextarea id="remark"  style="width:300px;height:100px;" effectDuration="400" value="#{salesForecastManagerBean.salesForecast.remark}">    
                                    </p:inputTextarea>
                                </td>
                            </tr>
                           
                        </table>

                        <hr/>
                        <center>
                            <p:commandButton id="btnDone" value="Done" onclick="confirmation2.show()" ajax="true" update="error" />
                        </center>
                    </p:panel>
                    
                     <p:confirmDialog message="You may only adjust the sales forecast one time. Adjusted sales forecast will assist the production department on production planning. Are you sure you want to proceed?"
                                     header="Adjusting a sales forecast..." severity="alert" widgetVar="confirmation2">
                        <p:commandButton value="Yes" update="quotationItemsPanel" oncomplete="confirmation.hide()"
                                         actionListener="#{salesForecastManagerBean.updateForecastPlan}"/>
                        <p:commandButton value="No" update="quotationItemsPanel" onclick="confirmation2.hide()" type="button"/>
                    </p:confirmDialog>


                </h:form>
            </ui:define>
        </ui:composition>
    </body>
</html>